<?php

namespace App\Http\Controllers\Api\Admin;

use Illuminate\Http\Request;
use App\Http\Business\CaptchasBusiness;
use App\Http\Requests\Api\Admin\CaptchaRequest;
use App\Http\Controllers\Api\Controller;

class CaptchasController extends Controller
{
    protected $captchaBusiness;

    public function __construct()
    {
        $this->captchaBusiness = new CaptchasBusiness();
    }

    public function store(CaptchaRequest $request)
    {
        $phone = $request->phone;

        $res = $this->captchaBusiness->build($phone);

        return $this->setStatusCode(201)->success($res);
    }

    public function show()
    {
        $isShow = $this->captchaBusiness->show( 'admin');
        $res = [];
        if ($isShow){
            $res = $this->captchaBusiness->build();
        }

        return $this->setStatusCode(201)->success($res);
    }

    public function verifyTip(Request $request)
    {
        $captchaKey = $request->input('captcha_key');
        $code = $request->input('code');
        if (!$captchaKey || !$code){
            abort(403, '验证字段必须填写');
        }

        $isCorrect = $this->captchaBusiness->verifyCaptcha($captchaKey, $code);
        $res = ['isCorrect' => $isCorrect ? 1 : 0];

        return $this->success($res);
    }

    public function verify(Request $request)
    {
        $captchaKey = $request->input('captcha_key');
        $code = $request->input('code');
        if (!$captchaKey || !$code){
            abort(403, '验证字段必须填写');
        }

        $isCorrect = $this->captchaBusiness->verifyCaptcha($captchaKey, $code, 2);
        $res = ['isCorrect' => $isCorrect ? 1 : 0];
        return $this->success($res);
    }
}
